Widgets Overview

The following is a run through of the widgets you can create with Interact. All widgets allow for the following keyword arguments:

  • label: Label to be shown next to the widget
  • value: The value the widget should be set to when created
  • signal: A signal object of type Reactive.Input which gets the value of the widget as user enters input.

Many of the widgets have keyword arguments specific to them. See below for more.


In [1]:
using Interact
using Reactive


Slider

Sliders are arguably the most useful of the widgets. A slider can be created with the slider{T <: Number}(range::Range{T}) function. The value of the slider defaults to the median of the range, and can be set using the value::T keyword argument. The type of signal a slider depends on the type of the range. E.g. A floating point range like 0:π/4:2π gives a signal of floating point values, while a range like 1:10 gives a signal of integers.


In [2]:
float_slider = slider(0:π/4:2π)


Out[2]:

In [3]:
int_slider = slider(1:10)


Out[3]:

In [4]:
signal(int_slider)


Out[4]:
5

Checkbox

checkbox takes an optional first argument which defaults to false and creates a checkbox.


In [5]:
display(checkbox())
checkbox(true)


Out[5]:

Toggle

You can create a toggle button with togglebutton which takes as an optional argument its label.


In [6]:
status = togglebutton("Mary called", value=true)


Out[6]:

In [7]:
map(s -> s ? "Mary called" : "Mary didn't call", signal(status))


Out[7]:
"Mary called"

Button

A button gives out a signal of a constant signal which is nothing by default. You can set this using the value keyword argument. The signal updates when the button is clicked.


In [8]:
b = button("Click Me")


Out[8]:

Here is how you can count the number of clicks made on a button using foldp on the signal, (think of foldp as fold over past values):


In [9]:
foldp((acc, value) -> acc + 1, 0, signal(b))


Out[9]:
0

Option widgets

There are 3 options widgets: dropdown, togglebuttons, radiobuttons. There are two types allowed as an argument while invoking these:

  1. AbstractArray (e.g. Vector, Tuple)
  2. Associative (e.g. Dict, OrderedDict) The default value is the first element (or undefined in case of an undordered Associative like Dict), but this can be set using the value keyword argument.

In [10]:
a = dropdown(["one", "two", "three"])


Out[10]:

In [11]:
signal(a)


Out[11]:
"one"

In [12]:
f = radiobuttons(Dict("Add" => +, "Sub" => -, "Exp" => ^))


Out[12]:

In [13]:
map(g -> g(e, π*im), signal(f))


Out[13]:
2.718281828459045 - 3.141592653589793im

Notice that the order "Add", "Sub", "Exp" was not retained in the above example, because a Dict does not save the ordering. To overcome this, we can use OrderedDict from DataStructures.jl package.


In [14]:
f_ = togglebuttons([("Add", +), ("Sub", -), ("Exp", ^)])


Out[14]:

In [15]:
map(g -> g(e, π*im), signal(f_))


Out[15]:
2.718281828459045 + 3.141592653589793im

Textbox

A textbox can be of a Number or AbstractString type. textbox takes one argument: its default value.


In [16]:
string_box = textbox("Change me")


Out[16]:

In [17]:
signal(string_box)


Out[17]:
"Change me"

A textbox can be of a Number type as well. Just set a default number value, or use textbox(typ=T) where T is a Number type.


In [18]:
int_box = textbox(0)


Out[18]:

In [19]:
signal(int_box)


Out[19]:
0

If creating a number typed textbox, you can also pass along an optional range field to set a bound on the possible values one can input. If an entered value exceeds the range, it is replaced by its nearest bounding number.


In [20]:
bounded_float_box = textbox(2pi, range=-10.0:10)


Out[20]:

In [21]:
signal(bounded_float_box)


Out[21]:
6.283185307179586

Textarea

textarea takes an optional default value and creates a textarea. Its signal changes when you type.


In [22]:
tex = textarea("Your very own \$\\LaTeX\$ editor")


Out[22]:

In [23]:
map(latex, signal(tex))


Out[23]:

The widget Function

widget tries to coerce a value into a widget.


In [24]:
map(display, [
    widget(1:10),                 # Slider
    widget(false),                # Checkbox
    widget("text"),               # Textbox
    widget(1.1),                  # Number Textbox
    widget([:on, :off]),          # Toggle Buttons
    widget(Dict( => float(π),  => 2π))
    ]);